'use strict';

angular.module('CBO').controller('RoutingController',
    ['$scope', '$stateParams', 'PublicModule', 'PublicFunction', 'BaseService', 'EntityService', 'PublicEnum', 'ProduceWorkEnum', '$location', 'Authentication',
    function($scope, $stateParams, PublicModule, PublicFunction, BaseService, EntityService, PublicEnum, ProduceWorkEnum, $location, Authentication) {
        $scope.DeliveryModeEnum = ProduceWorkEnum.DeliveryMode;
        $scope.PushPullModeEnum = ProduceWorkEnum.PushPullMode;

        BaseService.getExtendEnum('ItemCategory', function(extendEnum){
            $scope.ItemCategoryEnum = extendEnum;
        });

        $scope.btnSave = function(){
            if($scope.Module.Routing.FocusedRecord.Code === undefined || 
                $scope.Module.Routing.FocusedRecord.Code.trim() === ""){
                PublicFunction.alert("工艺路线：编码不能为空！", "保存提示");
                return;
            }
            if($scope.Module.Routing.FocusedRecord.Name === undefined || 
                $scope.Module.Routing.FocusedRecord.Name.trim() === ""){
                PublicFunction.alert("工艺路线：名称不能为空！", "保存提示");
                return;
            }
            if(($scope.Module.Routing.FocusedRecord.ItemMaster === undefined || 
                $scope.Module.Routing.FocusedRecord.ItemMaster == null) && 
                ($scope.Module.Routing.FocusedRecord.ItemCategory === undefined || 
                $scope.Module.Routing.FocusedRecord.ItemCategory == null)){
                PublicFunction.alert("工艺路线：料品和分类必须录入一个", "保存提示");
                return;
            }            

            if($scope.Module.Operation.Records.length > 0){
                for(var i=0; i<$scope.Module.Operation.Records.length; i++){
                    if($scope.Module.Operation.Records[i].Sequence === undefined || 
                        $scope.Module.Operation.Records[i].Sequence === null){
                        PublicFunction.alert("工序" + $scope.Module.Operation.Records[i].Sequence + "：序号不能为空！", "保存提示");
                        return;
                    }
                    if($scope.Module.Operation.Records[i].Name === undefined || 
                        $scope.Module.Operation.Records[i].Name.trim() === ""){
                        PublicFunction.alert("工序" + $scope.Module.Operation.Records[i].Sequence + "：名称不能为空！", "保存提示");
                        return;
                    }

                    for(var j=0; j<$scope.Module.Operation.Records.length; j++){
                        if(i !== j && $scope.Module.Operation.Records[i].Sequence === $scope.Module.Operation.Records[j].Sequence){
                            PublicFunction.alert("工序序号" + $scope.Module.Operation.Records[i].Sequence.toString() + "：不能重复！", "保存提示");
                            return;
                        }
                    }

                    if($scope.Module.Operation.Records[i].Work == undefined || 
                        $scope.Module.Operation.Records[i].Work == null){
                        PublicFunction.alert("工序" + $scope.Module.Operation.Records[i].Sequence + "：作业不能为空！", "保存提示");
                        return;
                    }
                }
            }
            else{
                PublicFunction.alert("工序行不能为空！", "工序");
                return;
            }

            if($scope.Module.Routing.FocusedRecord.RowStatus !== PublicEnum.RowStatus.UnChange){
                $scope.Module.Routing.save(function(){
                    for(var i=0; i<$scope.Module.Operation.Records.length; i++){
                        if($scope.Module.Operation.Records[i].RowStatus === PublicEnum.RowStatus.Insert){
                            $scope.Module.Operation.Records[i].Routing = $scope.Module.Routing.FocusedRecord._id;
                        }
                    }
                    $scope.Module.Operation.save();
                });
            }
            else{
                for(i=0; i<$scope.Module.Operation.Records.length; i++){
                    if($scope.Module.Operation.Records[i].RowStatus === PublicEnum.RowStatus.Insert){
                        $scope.Module.Operation.Records[i].Routing = $scope.Module.Routing.FocusedRecord._id;
                    }
                }
                $scope.Module.Operation.save();
            }
        };
        $scope.btnCancel = function(){
            $scope.Module.Routing.cancel(function(){
                loadOperation();
            });
        };
        $scope.btnAdd = function(){
            $scope.Module.Routing.add();
            $scope.Module.Routing.FocusedRecord.IsEffective = true;
        };
        $scope.btnDelete = function(){
            if($scope.Module.Routing.FocusedRecord.RowStatus !== PublicEnum.RowStatus.Insert){
                EntityService.delete('Operation', {Routing: $scope.Module.Routing.FocusedRecord._id}, function(){
                    $scope.Module.Operation.Records = [];
                    $scope.Module.Routing.delete();
                });
            }
            else{
                $scope.Module.Routing.delete();
                $scope.Module.Routing.emitRowsDeleteEvent($scope.Module.Operation.Records);
                $scope.Module.Operation.Records = [];
            }
        };
        $scope.btnList = function(){
            $location.url('/index/Routing/List');
        };
        var addRow = function(newRecord){
            var maxLineValue = 0;
            for(var i=0; i<$scope.Module.Operation.Records.length; i++){
                if($scope.Module.Operation.Records[i].Sequence !== "" && 
                    $scope.Module.Operation.Records[i].Sequence !== undefined && 
                    $scope.Module.Operation.Records[i].Sequence > maxLineValue)
                {
                    maxLineValue = $scope.Module.Operation.Records[i].Sequence;
                }
            }
            newRecord.Sequence = maxLineValue + 10;
            newRecord.Routing = $scope.Module.Routing.FocusedRecord._id;
            newRecord.IsCheckPoint = true;
            newRecord.IsEffective = true;
        };
        $scope.cellValueChange = function(columnName, newValue, oldValue){
            if(columnName === "Work"){
                if(newValue.length > 0){
                    $scope.Module.Operation.FocusedRecord.IsTiming = newValue[0].IsTiming;
                    $scope.Module.Operation.FocusedRecord.IsQCpoint = newValue[0].IsQCpoint;
                    $scope.Module.Operation.FocusedRecord.IsMultScan = newValue[0].IsMultScan;
                    $scope.Module.Operation.FocusedRecord.IsStoragePoint = newValue[0].IsStoragePoint;
                }
                else{
                    $scope.Module.Operation.FocusedRecord.IsTiming = false;
                    $scope.Module.Operation.FocusedRecord.IsQCpoint = false;
                    $scope.Module.Operation.FocusedRecord.IsMultScan = false;
                    $scope.Module.Operation.FocusedRecord.IsStoragePoint = false;
                }
                $scope.Module.Operation.emitRowsDataChangeEvent([$scope.Module.Operation.FocusedRecord]);
            }
        };

        var loadOperation = function(){
            if($scope.Module.Routing.FocusedRecord !== null && 
                $scope.Module.Routing.FocusedRecord.RowStatus !== PublicEnum.RowStatus.Insert){
                //$scope.Module.Operation.Condition = {Routing: $scope.Module.Routing.FocusedRecord._id};
                $scope.Module.Operation.load();
            }
            else{
                $scope.Module.Operation.Records = [];
            }            
        };

        //初始化
        PublicModule.initModule($scope, 'Routing');
        $scope.Module.Routing.Populate = {path: 'ItemMaster', select: 'Code Name'};
        PublicModule.initModule($scope, 'Operation');
        $scope.Module.Operation.PageSize = 10;
        $scope.Module.Operation.LoadMode = PublicEnum.LoadMode.CacheAndNoPaging;
        $scope.Module.Operation.Populate = {path: 'Work', select: 'Code Name'};
        $scope.Module.Operation.OrderBy = {Sequence: 1};
        $scope.Module.Operation.ParentModule = $scope.Module.Routing;
        $scope.Module.Operation.AfterOnAdd = addRow;

        if($stateParams.id) {
            $scope.Module.Routing.findById($stateParams.id, function(record){
                $scope.Module.Routing.FocusedRecord = record;
                //loadOperation();
            });
        }
        else{
            $scope.btnAdd();
        }
    }
]);
